package thread;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Demo2
 * Package: thread
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-10
 * Time: 11:14
 */

//创建线程池
public class Demo2 {

    public static void main(String[] args) {

        //1.提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor s = (ThreadPoolExecutor) service;
        //设置线程池属性 设置线程池中线程数量的上线
        s.setMaximumPoolSize(50);

        //2.指定线程的操作
        service.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=1000; i++) {
                    if (i % 2 != 0){
                        System.out.println(  Thread.currentThread().getName() +" i= " + i);
                    }
                }
            }
        });

        service.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=1000; i++) {
                    if (i % 2 == 0){
                        System.out.println(  Thread.currentThread().getName() +" i= " + i);
                    }
                }
            }
        });

        //关闭线程池
        service.shutdown();


    }

}
